package com.ehome.user.Activity.Login;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ehome.user.Module.user;
import com.ehome.user.R;
import com.ehome.user.Utils.PreferencesUtils;
import com.ehome.user.Utils.TimeCount;
import com.ehome.user.Utils.Tools;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2018/3/7 0007.
 */

public class LoginActivity extends AppCompatActivity implements TextWatcher {
    @Bind(R.id.tvGoodLogin)
    TextView tvGoodLogin;
    @Bind(R.id.tvPwdLogin)
    TextView tvPwdLogin;
    @Bind(R.id.etnPhoneNumber)
    EditText etnPhoneNumber;
    @Bind(R.id.etnPwd)
    EditText etnPwd;
    @Bind(R.id.iv_look)
    ImageView ivLook;
    @Bind(R.id.rl_look)
    RelativeLayout rlLook;
    @Bind(R.id.btnLogin)
    Button btnLogin;
    @Bind(R.id.tvRegister)
    TextView tvRegister;
    @Bind(R.id.tvForPwd)
    TextView tvForPwd;
    @Bind(R.id.ivLoginWc)
    ImageView ivLoginWc;
    @Bind(R.id.ivLoginQq)
    ImageView ivLoginQq;
    @Bind(R.id.rlPwd)
    RelativeLayout rlPwd;
    @Bind(R.id.pwdView)
    View pwdView;
    @Bind(R.id.rlPassLayout)
    RelativeLayout rlPassLayout;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.textView1)
    TextView textView1;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.ll_yanzm)
    RelativeLayout ll_yanzm;
    @Bind(R.id.ed_yzm)
     EditText ed_yzm;
    @Bind(R.id.tv_yanzengm)
    TextView tv_yanzengm;
    private TimeCount time;
    String isOne = "true";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        tvGoodLogin.setTextSize(18);
        tvGoodLogin.setTextColor(getResources().getColor(R.color.A121212));

        time = new TimeCount(LoginActivity.this, 60000, 1000, tv_yanzengm);
        etnPhoneNumber.addTextChangedListener(this);
        etnPwd.addTextChangedListener(this);
        btnLogin.setClickable(false);
    }

    @OnClick({R.id.tvGoodLogin, R.id.tvPwdLogin,R.id.btnLogin,R.id.tvForPwd,R.id.tvRegister,R.id.tv_yanzengm})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.tvGoodLogin:
                tvGoodLogin.setTextSize(18);
                tvGoodLogin.setTextColor(getResources().getColor(R.color.A121212));
                tvPwdLogin.setTextSize(14);
                tvPwdLogin.setTextColor(getResources().getColor(R.color.A999999));
                rlPwd.setVisibility(View.GONE);
                ll_yanzm.setVisibility(View.VISIBLE);
                tvForPwd.setText("收不到?");
                rlPassLayout.setVisibility(View.GONE);
                isOne = "true";
                if (etnPhoneNumber.getText().toString().length() > 0) {
                    btnLogin.setBackgroundResource(R.drawable.btn_selet_type);
                    btnLogin.setClickable(true);
                } else {
                    btnLogin.setBackgroundResource(R.drawable.btn_type);
                    btnLogin.setClickable(false);
                }
                break;
            case R.id.tvPwdLogin:
                tvPwdLogin.setTextSize(18);
                tvPwdLogin.setTextColor(getResources().getColor(R.color.A121212));
                tvGoodLogin.setTextSize(14);
                tvGoodLogin.setTextColor(getResources().getColor(R.color.A999999));
                btnLogin.setBackgroundResource(R.drawable.btn_type);
                btnLogin.setClickable(false);
                rlPwd.setVisibility(View.VISIBLE);
                ll_yanzm.setVisibility(View.GONE);
                rlPassLayout.setVisibility(View.VISIBLE);
                btnLogin.setText("登录");
                tvForPwd.setText("忘记密码?");
                isOne = "false";
                break;
            case R.id.btnLogin:
                if (isOne.equals("true")) {

                    if (TextUtils.isEmpty(etnPhoneNumber.getText().toString())) {
                        Toast.makeText(LoginActivity.this,"请先输入手机号",Toast.LENGTH_SHORT).show();
                        return;
                    } else {
                        if (!Tools.isMobileNumber(etnPhoneNumber.getText().toString())) {
                            Toast.makeText(LoginActivity.this,"电话号码格式不正确",Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    if(TextUtils.isEmpty(ed_yzm.getText().toString())){
                        Toast.makeText(LoginActivity.this,"请先输入验证码",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    BmobSMS.verifySmsCode(etnPhoneNumber.getText().toString(), ed_yzm.getText().toString(), new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            if(e==null){
                                Toast.makeText(LoginActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                                PreferencesUtils.putBoolean(getApplicationContext(),"tag",true);
                                PreferencesUtils.putString(getApplicationContext(),"phone",etnPhoneNumber.getText().toString());

                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this,"验证失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    //getPhoneCode(etnPhoneNumber.getText().toString().trim());
                } else {
                    if (TextUtils.isEmpty(etnPhoneNumber.getText().toString().trim())) {
                        Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (TextUtils.isEmpty(etnPwd.getText().toString().trim())) {
                        Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    login();
                }
                break;
            case R.id.tvForPwd://忘记密码
                Intent intent=new Intent();
                intent.putExtra("pastag","1");
                intent.setClass(LoginActivity.this,ForgetPasswodActivity.class);
                startActivity(intent);
                break;
            case R.id.tvRegister://账号注册
                Intent intent1=new Intent();
                intent1.putExtra("pastag","2");
                intent1.setClass(LoginActivity.this,ForgetPasswodActivity.class);
                startActivity(intent1);
                break;
            case R.id.tv_yanzengm:
                if (TextUtils.isEmpty(etnPhoneNumber.getText().toString().trim())) {
                    Toast.makeText(this,"请输入手机号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!Tools.isMobileNumber(etnPhoneNumber.getText().toString())) {
                    Toast.makeText(this,"电话号码格式不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                BmobSMS.requestSMSCode(etnPhoneNumber.getText().toString(), "易家宝", new QueryListener<Integer>() {
                    @Override
                    public void done(Integer integer, BmobException e) {
                        if(e==null){
                            Toast.makeText(LoginActivity.this,"发送短信成功",Toast.LENGTH_SHORT).show();
                            time.start();
                        }else {
                            Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
    }

    private void login() {
        BmobQuery<user> query = new BmobQuery<user>();
        query.addWhereEqualTo("phone",etnPhoneNumber.getText().toString());
        query.addWhereEqualTo("password",etnPwd.getText().toString());
        query.findObjects(new FindListener<user>() {
            @Override
            public void done(List<user> list, BmobException e) {
                if(e==null){
                    //Toast.makeText(LoginActivity.this,list.get(0).getPhone(),Toast.LENGTH_SHORT).show();
                    PreferencesUtils.putBoolean(getApplicationContext(),"tag",true);
                    PreferencesUtils.putString(getApplicationContext(),"phone",etnPhoneNumber.getText().toString());
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this,"账户或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (isOne.equals("true")) {
            if (!TextUtils.isEmpty(etnPhoneNumber.getText().toString())
                    && etnPhoneNumber.getText().toString().trim().length() > 10) {
                btnLogin.setBackgroundResource(R.drawable.btn_selet_type);
                btnLogin.setClickable(true);
            } else {
                btnLogin.setBackgroundResource(R.drawable.btn_type);
                btnLogin.setClickable(false);
            }
        } else {
            if (!TextUtils.isEmpty(etnPhoneNumber.getText().toString())
                    && etnPhoneNumber.getText().toString().trim().length() > 10
                    && !TextUtils.isEmpty(etnPwd.getText().toString())) {
                btnLogin.setBackgroundResource(R.drawable.btn_selet_type);
                btnLogin.setClickable(true);
            } else {
                btnLogin.setBackgroundResource(R.drawable.btn_type);
                btnLogin.setClickable(false);
            }
        }
    }
    @Override
    public void afterTextChanged(Editable s) {

    }
}
